package com.example.esp32;

import static android.content.ContentValues.TAG;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

public class itmeaddr extends AppCompatActivity {
    String text, id;
    private LinearLayout mContainer;
    private Button button;
    private TextView textview2;
    private ImageButton tc, tj, buttonS;
    private View views;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_itmeaddr);
        mContainer = findViewById(R.id.gl);
        Intent intent = getIntent();
        text = intent.getStringExtra("myText");
        id = intent.getStringExtra("id");

        cxpost(id);
        tc = findViewById(R.id.TUC);
        tj = findViewById(R.id.add);
        tc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        button = (Button) findViewById(R.id.button);
        tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addview("Button", "ss", id);
                cxpost(id);
                Toast.makeText(itmeaddr.this, text + id, Toast.LENGTH_LONG).show();


            }
        });}

    private void addview(String view, String gn, String viewid) {
        new Thread() {
            @Override
            public void run() {
                super.run();

                request request = new request();
                JSONObject jsonObject = new JSONObject();

                try {
                    jsonObject.put("view", view);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

                try {
                    jsonObject.put("gn", gn);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                try {
                    jsonObject.put("viewid", viewid);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

                String a;
                try {
                    a = jsonObject.toString(1);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                String data = a;
                String resq = request.post("http://ipzhangt:8999/addview", data);
                Log.d(TAG, resq);
                Log.d(TAG, data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                    }
                });
            }
        }.start();


    }

    private void cxpost(String viewid) {
        new Thread() {
            @Override
            public void run() {
                super.run();

                request request = new request();
                JSONObject jsonObject = new JSONObject();

                try {
                    jsonObject.put("viewid", viewid);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }


                String a;
                try {
                    a = jsonObject.toString(1);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                String data = a;
                String resq = request.post("http://ipzhangt:8999/cxview", data);
                Log.d(TAG, resq);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObjectc = new JSONObject(resq);
                            int itme = jsonObjectc.optInt("itme");
                            if (itme == 0) {
                                Toast.makeText(itmeaddr.this, "没有设备", Toast.LENGTH_LONG).show();
                            }
                            for (int i = 0; i < itme; i++) {
                                String aadd = jsonObjectc.optString("data" + i);

                                JSONObject jsonObject = new JSONObject(aadd);
                                String zt = jsonObject.optString("view");
                                String idd = jsonObject.optString("id");

                                    views = LayoutInflater.from(itmeaddr.this).inflate(R.layout.button, null, false);
                                    buttonS = (ImageButton) views.findViewById(R.id.button2sasd);
                                    buttonS.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Toast.makeText(itmeaddr.this, "点击", Toast.LENGTH_LONG).show();
                                            DragViewUtil.registerDragAction(buttonS,views);
                                        }
                                    });
                                    mContainer.addView(views);
                                Log.d(TAG, resq);
                            }
                            // 封装Java对象
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }.start();
    }

    public void postviewa(String zhuti, String xx) {
        new Thread() {
            @Override
            public void run() {
                super.run();

                request request = new request();
                JSONObject jsonObject = new JSONObject();

                try {
                    jsonObject.put("zhuti", zhuti);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                try {
                    jsonObject.put("xx", xx);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }


                String a;
                try {
                    a = jsonObject.toString(1);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                String data = a;
                String resq = request.post("http://ipzhangt:8999/", data);
                Log.d(TAG, resq);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        }.start();

    }
}